package com.chatsocket.socketServlet;

import java.io.IOException;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;

import com.chatsocket.util.Constant;

/**
 *webSocket管理类 
 */
public class WebSocketUtils {
	/**
	 * 
	 * @param userRemark 用户标记
	 * @param echoBody 提醒内容
	 */
	public static void notifyUser(String userRemark,String echoBody){
		CopyOnWriteArraySet<WebSocketServlet> set = Constant.CURRENT_ONLINE_WEBSOCKET.get(userRemark);
		if(set==null||set.size()==0){
			return;
		}
		for(WebSocketServlet webSocket:set){
			try {
				webSocket.sendMessage(echoBody);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static List<String> getSessionIdsByDestination(String destination) {
		CopyOnWriteArraySet<WebSocketServlet> set = Constant.CURRENT_ONLINE_WEBSOCKET.get(destination);
		List<String> httpSessionIds=new CopyOnWriteArrayList<String>();
		if(set==null||set.size()==0){
			return null;
		}
		for(WebSocketServlet webSocket:set){
			httpSessionIds.add(webSocket.getHttpSessionId());
		}
		return httpSessionIds;
	}
}
